﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamOPSchoolCup.Domain.POCO.Members;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;
using System.Web.Security;
using System.Web;
using System.Data.Entity.Infrastructure;

namespace TeamOPSchoolCup.Domain.Services
{
  public class MemberService
  {
    public static bool AddStudent(Student student)
    {
      bool s;
      using (EFDbContext db = new EFDbContext())
      {
        s = AddStudent(db, student);
      }

      return s;
    }

    public static bool AddStudent(EFDbContext db, Student student)
    {
      db.Members.Add(student.Member);
      db.SaveChanges();
      student.MemberId = student.Member.MemberId;
      db.Students.Add(student);
      return (db.SaveChanges() > 0);
    }

    public static bool AddCoach(Coach coach)
    {
      bool c;
      using (EFDbContext db = new EFDbContext())
      {
        c = AddCoach(db, coach);
      }

      return c;
    }

    public static bool AddCoach(EFDbContext db, Coach coach)
    {
      db.Members.Add(coach.Member);
      db.SaveChanges();
      coach.MemberId = coach.Member.MemberId;
      db.Coaches.Add(coach);
      return (db.SaveChanges() > 0);
    }

    public static bool AddNSSFRep(NSSFRep nssfrep,NSSF nssf)
    {
      bool n;
      using (EFDbContext db = new EFDbContext())
      {
        n = AddNSSFRep(db, nssfrep,nssf);
      }

      return n;
    }

    public static bool AddNSSFRep(EFDbContext db, NSSFRep nssfrep,NSSF nssf)
    {
      db.Members.Add(nssfrep.Member);
      db.SaveChanges();
      nssfrep.MemberId = nssfrep.Member.MemberId;
      db.NSSFReps.Add(nssfrep);
      if (NSSFService.AddNSSFRep(db, nssfrep, nssf))
      {
        return (db.SaveChanges() > 0);
      }
      else
      {
        db.NSSFReps.Remove(db.NSSFReps.Single(n => n.NSSFId  == nssfrep.NSSFId));
        db.Members.Remove(db.Members.Single(m => m.MemberId == nssfrep.MemberId));
        db.SaveChanges();
        return false;
      }
    }

    public static bool AddSCC(SCC scc)
    {
      bool s;
      using (EFDbContext db = new EFDbContext())
      {
        s = AddSCC(db, scc);
      }

      return s;
    }

    public static bool AddSCC(EFDbContext db, SCC scc)
    {
      db.Members.Add(scc.Member);
      db.SaveChanges();
      scc.MemberId = scc.Member.MemberId;
      db.SCCs.Add(scc);
      return (db.SaveChanges() > 0);
    }

    public static bool AddISFAdmin(ISFAdmin isfadmin)
    {
      bool i;
      using (EFDbContext db = new EFDbContext())
      {
        i = AddISFAdmin(db, isfadmin);
      }

      return i;
    }

    public static bool AddISFAdmin(EFDbContext db, ISFAdmin isfadmin)
    {
      db.Members.Add(isfadmin.Member);
      db.SaveChanges();
      isfadmin.MemberId = isfadmin.Member.MemberId;
      db.ISFAdmins.Add(isfadmin);
      return (db.SaveChanges() > 0);
    }

    /*public Member makeMember(string name,DateTime birthDate,string street, string zipcode, string city, string email, string language, bool gender,string phoneNumber)
    {

      Member member = new Member { Name = name, 
                          Street = street, 
                          City = city, 
                          Zipcode = zipcode, 
                          Email = email, 
                          Language = language ,
                          Gender = gender, 
                          BirthDate = birthDate, 
                          PhoneNumber = phoneNumber };
      return member;
    }*/

    public static bool ChangeLanguage(Member member, string newLanguage)
    {
      bool l;
      using (EFDbContext db = new EFDbContext())
      {
        l = ChangeLanguage(db, member, newLanguage);
      }

      return l;
    }

    public static bool ChangeLanguage(EFDbContext db, Member member, string newLanguage)
    {
      Member mem = db.Members.Where(m => m.MemberId == member.MemberId).First();
      mem.Language = newLanguage;

      return (db.SaveChanges() > 0);
    }

    public static bool ChangeName(Member member, string newName)
    {
      bool n;
      using (EFDbContext db = new EFDbContext())
      {
        n = ChangeName(db, member, newName);
      }

      return n;
    }

    public static bool ChangeName(EFDbContext db, Member member, string newName)
    {
      Member mem = db.Members.Where(m => m.MemberId == member.MemberId).First();
      mem.Name = newName;

      return (db.SaveChanges() > 0);
    }

    public static bool ChangeEmail(Member member, string newEmail)
    {
      bool e;
      using (EFDbContext db = new EFDbContext())
      {
        e = ChangeEmail(db, member, newEmail);
      }

      return e;
    }

    public static bool ChangeEmail(EFDbContext db, Member member, string newEmail)
    {
      Member mem = db.Members.Where(m => m.MemberId == member.MemberId).First();
      mem.Email = newEmail;

      return (db.SaveChanges() > 0);
    }

    public static bool ChangePassword(Member member, string oldPassword, string newPassword)
    {
      bool p;
      using (EFDbContext db = new EFDbContext())
      {
        p = ChangePassword(db, member, oldPassword, newPassword);
      }

      return p;
    }

    public static bool ChangePassword(EFDbContext db, Member member, string password, string newPassword)
    {

      Member mem = db.Members.Where(m => m.MemberId == member.MemberId).First();
      MembershipUser user = Membership.GetUser(mem.UserId);
      user.ChangePassword(password, newPassword);

      return (db.SaveChanges() > 0);
    }

    public static Region GetRegion(Member member)
    {
      Region r;
      using (EFDbContext db = new EFDbContext())
      {
        r = GetRegion(db, member);
      }

      return r;
    }

    public static Region GetRegion(EFDbContext db, Member member)
    {
      //Region region = db.Regions.Include("NSSF.Teams.TeamMemberships.Student.Member").Where(m => m.NSSF.Teams.TeamMemberships.Student.Member.MemberId==member.MemberId));
      Region region = db.Regions.Where(r => r.RegionId == 1).First();
      return region;
    }

    /*
     * Coaches
     */
    public static List<Coach> GetCoaches()
    {
      List<Coach> coaches;
      using(EFDbContext db = new EFDbContext()){
        coaches = GetCoaches(db);
      }

      return coaches;
    }

    public static List<Coach> GetCoaches(EFDbContext db)
    {
      return db.Coaches.ToList<Coach>();
    }

    public static List<Coach> GetCoachesAsMember()
    {
      List<Coach> coaches;
      using(EFDbContext db = new EFDbContext()){
        coaches = GetCoachesAsMember(db);
      }

      return coaches;
    }

    public static List<Coach> GetCoachesAsMember(EFDbContext db)
    {
      return db.Coaches.Include("Member").ToList<Coach>();
    }

    /*
     * Students
     */
    public static List<Student> GetStudents()
    {
      List<Student> students;
      using (EFDbContext db = new EFDbContext())
      {
        students = GetStudents(db);
      }

      return students;
    }

    public static List<Student> GetStudents(EFDbContext db)
    {
      return db.Students.ToList<Student>();
    }

    public static List<Student> GetStudentsAsMember()
    {
      List<Student> students;
      using (EFDbContext db = new EFDbContext())
      {
        students = GetStudentsAsMember(db);
      }

      return students;
    }

    public static List<Student> GetStudentsAsMember(EFDbContext db)
    {
      return db.Students.Include("Member").ToList<Student>();
    }

    /*
     * NSSFRep
     */
    public static List<NSSFRep> GetNSSFReps()
    {
      List<NSSFRep> nssfreps;
      using (EFDbContext db = new EFDbContext())
      {
        nssfreps = GetNSSFReps(db);
      }

      return nssfreps;
    }

    public static List<NSSFRep> GetNSSFReps(EFDbContext db)
    {
      return db.NSSFReps.ToList<NSSFRep>();
    }

    public static List<NSSFRep> GetNSSFRepsAsMember()
    {
      List<NSSFRep> nssreps;
      using (EFDbContext db = new EFDbContext())
      {
        nssreps = GetNSSFRepsAsMember(db);
      }

      return nssreps;
    }

    public static List<NSSFRep> GetNSSFRepsAsMember(EFDbContext db)
    {
      return db.NSSFReps.Include("Member").ToList<NSSFRep>();
    }
    public static Member GetMember(int memberId)
    {
      Member m;
      using(EFDbContext db = new EFDbContext()){
        m = GetMember(db, memberId);
      }

      return m;
    }

    public static Member GetMember(EFDbContext db, int memberId)
    {
      return db.Members.Find(memberId);
    }

    public static Coach GetCoach(string name)
    {
      Coach c;
      using (EFDbContext db = new EFDbContext())
      {
        c=GetCoach(db, name);
      }
      return c;
    }

    public static Coach GetCoach(EFDbContext db, string name)
    {
      return db.Coaches.Include("Member").Single(x => x.Member.Name == name);
    }

    public static Student GetStudent(string name)
    {
      Student s;
      using (EFDbContext db = new EFDbContext())
      {
        s = GetStudent(db, name);
      }
      return s;
    }

    public static Student GetStudent(EFDbContext db, string name)
    {
      return db.Students.Include("Member").Single(x => x.Member.Name == name);
    }
    public static NSSFRep GetNSSFRep(string name)
    {
      NSSFRep n;
      using (EFDbContext db = new EFDbContext())
      {
        n = GetNSSFRep(db, name);
      }
      return n;
    }

    public static NSSFRep GetNSSFRep(EFDbContext db, string name)
    {
      return db.NSSFReps.Include("Member").Single(x => x.Member.Name == name);
    }
  }
}
